/*

	作者：怼烎
	QQ：1395961821
	时间：2017.10.10

*/


$(function () {

    /*$(".del").click(function(){

    	console.log(this);
      layer.confirm('是否确定删除此商品?', {icon: 3, title:'删除'}, function(index){
    	//do something
      	layer.close(index);
      });
    });*/

	
	var reduce = $(".reduce");
	var shopNumber = $(".shopNumber");
	var plus = $(".plus");

	/*默认禁减*/
	reduce.attr("disabled",true);

	$.each(plus,function (i) {
		$(this).click(function () {
			shopNumber.eq(i).val(Math.abs(parseInt(shopNumber.eq(i).val()))+1);
			if (parseInt(shopNumber.eq(i).val())!=1) {
            	reduce.eq(i).attr("disabled",false);
			}
		});
	});

	$.each(reduce,function (i) {
		$(this).click(function () {
			shopNumber.eq(i).val(Math.abs(parseInt(shopNumber.eq(i).val()))-1);
			if (parseInt(shopNumber.eq(i).val())==1) {
            	reduce.eq(i).attr("disabled",true);
			}
		});
	});




	var check = $(".xuan");
	var inu = $(".i");


	var quan = $("#quan");
	var qu = $("#qu");
 


	/*console.log(check);*/
	$.each(check,function (i) {
		$(this).click(function () {
			if ($(this).is(':checked')) {
				if (check.is(":checked")) {
					inu.eq(i).attr("class","ihover");
				}
				/*console.log($(this).is(':checked'));*/
			}else{
				inu.eq(i).attr("class","i");
				quan.attr("checked",false);
				qu.attr("id","qu");
				/*console.log($(this).is(':checked'));*/
			}
		});
	});




	quan.click(function(){
		$.each(check,function(i){
			if (quan.is(":checked")) {
				$(this).attr("checked",true);
				inu.eq(i).attr("class","ihover");
				qu.attr("id","quhover");
			}else{
				$(this).attr("checked",false);
				inu.eq(i).attr("class","i");
				qu.attr("id","qu");
			}
		});
	});




	//创建一个购物车对象
	function Cart(){
		//先判断是否已经是订单
		this.is_order=function(){
		    data_length = $(".xuan").length;
		   	for(var i=0;i<data_length;i++){
		   		if($(".xuan")[i].checked==true){
		   	    /*$(".xuan")[i].nextSibling.className = "ihover";*/
		   	    $(".xuan")[i].nextSibling.className = "ihover";
		   	    }
		    }
		}

		//订单总价钱	
		this.get_total_price=function(){
			var url = '/index.php/Home/Cart/get_total_price';
    		$.ajax({
    			type : "GET",
    			url  : url,
    			error : function(requeste){
					alert("1");
   				    return;
    			},
    			success : function(v){
					$("#total_price").html("¥"+parseInt(v));
    			}
    		});
		}

		//当商品数量发生变化时，发送数据
		my_this = this;
		this.when_chang_num=function(){
			$('.reduce').on('click',function(){
		    //将购物车表id和数量给发送后台
		    var id = $(this).parent().parent().parent().parent().siblings(".xuan").attr("title");
		    var value = $(this).siblings(".shopNumber").val();
		    var url = '/index.php/Home/Cart/get_num/cart_id/'+id+"/goods_num/"+value;
		    var _this = $(this);
   		    $.ajax({
   		        type : "GET",
   		        url  : url,
   		        error: function(request) {
   		            alert("2");
   		        return;
   		    	},
   		    	success: function(v) {
   		    		_this.parent().parent().parent().parent().siblings(".xuan").attr("total_price",parseInt(v));
   		    		my_this.get_total_price();
   		    	}
   		        });
			});

			$('.plus').on('click',function(){
		    //将购物车表id和数量给发送后台
		    var id = $(this).parent().parent().parent().parent().siblings(".xuan").attr("title");
		    var value = $(this).siblings(".shopNumber").val();
		    var url = '/index.php/Home/Cart/get_num/cart_id/'+id+"/goods_num/"+value;
		    var _this = $(this);
   		    $.ajax({
   		        type : "GET",
   		        url  : url,
   		        error: function(request) {
   		            alert("3");
   		        return;
   		    	},
   		    	success: function(v) {
   		    		_this.parent().parent().parent().parent().siblings(".xuan").attr("total_price",parseInt(v));
   		    		my_this.get_total_price();
   		    	}
   		        });
			});


		}

		//当点击选择按钮时
		this.when_click_input=function(){
			$('.xuan').on('click',function(){
		
				//判断商品是否被选中
				//如果商品被选中，则is_order为1。否则is_order为0
		
				if(this.checked == false){
					//如果没有被选中，则获取当前的name值
					var this_title = this.title;
					var url = '/index.php/Home/Cart/cancel_order/cart_id/'+this_title;
   					$.ajax({
   				    	type : "GET",
   				    	url  : url,
   				    	error: function(request) {
   				    	    alert("5");
   				    	    return;
   				    	},
   				    	success: function(v) {
   				    		my_this.get_total_price();
   				    	}
   					});
				}else{
					var url = '/index.php/Home/Cart/enter';
   					$.ajax({
   				    	type : "POST",
   				    	url  : url,
   				    	data : $("#cart_post").serialize(),
   				    	error: function(request) {
   				    	    alert("6");
   				    	    return;
   				    	},
   				    	success: function(v) {
   				    		my_this.get_total_price();
   				    	}
   					});
				}
			});

			//点击全选时
			$('#quan').on('click',function(){
				if(this.checked == true){
		  		var url = '/index.php/Home/Cart/checked_all';
   		  		$.ajax({
   		  			type : "GET",
   		  			url  : url,
   		  			error: function(request) {
   		  			    alert("7");
   		  			    return;
   		  			},
   		  			success: function(v) {
   		  				my_this.get_total_price();
   		  			}
   				});
   		  	   }else{
		  		 var url = '/index.php/Home/Cart/no_checked_all';
   		  		 $.ajax({
   		  		 	type : "GET",
   		  		 	url  : url,
   		  		 	error: function(request) {
   		  		 	    alert("8");
   		  		 	    return;
   		  		 	},
   		  			success: function(v) {
   		  				my_this.get_total_price();
   		  			}
   				 });
   		  	   }
   		 });
	}

	 //主动改变数值时
	this.change_num = function(){
		 var shopNumber = $(".shopNumber");
		 $.each(shopNumber,function(){
		 	$(this).change(function(){
		 		 var id = $(this).parent().parent().parent().parent().siblings(".xuan").attr("title");
		         var value = $(this).val();
		 		 var url = '/index.php/Home/Cart/get_num/cart_id/'+id+"/goods_num/"+value;
		    	 var _this = $(this);
   		    	 $.ajax({
   		    	     type : "GET",
   		    	     url  : url,
   		    	     error: function(request) {
   		    	         alert("9");
   		    	     return;
   		    	 	},
   		    	 	success: function(v) {
   		    	 		_this.parent().parent().parent().parent().siblings(".xuan").attr("total_price",parseInt(v));
   		    	 		my_this.get_total_price();
   		    	 	}
   		    	 });
			 });
				my_this.get_total_price();
		});
	}	

	//删除购物车商品时
	this.delete_goods = function(){
		var del = $('.del');
    	$.each(del,function (){
    		$(this).click(function(){
    			var _this = this;
    			var __this = $(this);
    			layer.confirm('是否确定删除此商品?', {icon: 3, title:'删除'},function(index){
    				//删除节点
    				__this.parent().parent().parent().parent().parent().remove();
    				var url = _this.title;
    				$.ajax({
    					type:"get",
    					url : url,
    				})
    				my_this.get_total_price();
    				layer.close(index);
    			})
    			return false;
    		});
    	});
	}
}
	var myCart = new Cart();
	myCart.is_order();
	myCart.get_total_price();
	myCart.when_chang_num();
	myCart.when_click_input();
	myCart.change_num();
	myCart.delete_goods();

});